package com.crm.service.impl.sys;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.sys.ImportFailureLogCustomMapper;
import com.crm.dao.mapper.sys.ImportFailureLogMapper;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactExample;
import com.crm.model.entity.sys.ImportFailureLog;
import com.crm.model.entity.sys.ImportFailureLogExample;
import com.crm.model.qo.sys.ImportFailureLogQO;
import com.crm.model.vo.sys.ImportFailureLogVO;
import com.crm.service.sys.ImportFailureLogDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/5/25 8:41
 */
@Service
public class ImportFailureLogDbServiceImpl implements ImportFailureLogDbService {
    @Autowired
    ImportFailureLogMapper importFailureLogMapper;

    @Autowired
    ImportFailureLogCustomMapper importFailureLogCustomMapper;

    /**
     * 插入导入失败日志
     * @Author pym
     * @Date  2021/5/25 8:44
     * @param importFailureLog 导入失败日志
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean insertImportFailureLog(ImportFailureLog importFailureLog) {
        Boolean result = false;
        int num = importFailureLogMapper.insert(importFailureLog);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 客户冲突数据列表
     * @Author pym
     * @Date  2021/5/28 11:13
     * @param importFailureLogQO -查询条件
     * @param accountId -账号ID
     * @return {@link {java.util.List<com.crm.model.vo.sys.ImportFailureLogVO>}}
     */
    @Override
    public List<ImportFailureLogVO> selectCustomerConflictList(ImportFailureLogQO importFailureLogQO, String accountId) {
        return importFailureLogCustomMapper.selectCustomerConflictList(importFailureLogQO,accountId);
    }

    /**
     * 客户导入失败数据列表
     * @Author pym
     * @Date  2021/5/28 14:00
     * @param importFailureLogQO -查询条件
     * @param accountId -账号ID
     * @return {@link {java.util.List<com.crm.model.vo.sys.ImportFailureLogVO>}}
     */
    @Override
    public List<ImportFailureLogVO> selectImportFailedDataList(ImportFailureLogQO importFailureLogQO, String accountId) {
        return importFailureLogCustomMapper.selectImportFailedDataList(importFailureLogQO,accountId);
    }

    /**
     * 订单导入失败数据列表
     * @Author pym
     * @Date  2021/6/8 15:07
     * @param importFailureLogQO -查询条件
     * @param accountId -账号ID
     * @return {@link {java.util.List<com.crm.model.vo.sys.ImportFailureLogVO>}}
     */
    @Override
    public List<ImportFailureLogVO> selectOrderImportFailedDataList(ImportFailureLogQO importFailureLogQO, String accountId) {
        return importFailureLogCustomMapper.selectOrderImportFailedDataList(importFailureLogQO,accountId);
    }

    @Override
    public List<ImportFailureLog> selectImportFailureLog(String content, String type, String companyId, String accountId) {
        ImportFailureLogExample importFailureLogExample = new ImportFailureLogExample();
        ImportFailureLogExample.Criteria criteria = importFailureLogExample.createCriteria();
        criteria.andReasonForFailureEqualTo(content);
        criteria.andOperateTypeEqualTo(type);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andCompanyIdEqualTo(companyId);
        List<ImportFailureLog> list = importFailureLogMapper.selectByExample(importFailureLogExample);
        return list;
    }

    /**
     * 查询日志记录
     * @param importName
     * @param operateType
     * @param companyId
     * @param accountId
     * @return
     */
    @Override
    public List<ImportFailureLogVO> selectImportFailureLogByNameAndType(String importName, Integer operateType, String companyId, String accountId) {
        return importFailureLogCustomMapper.selectImportFailureLogByNameAndType(importName,operateType,companyId,accountId);
    }

    @Override
    public Boolean selectImportFailureLogByExample(String operateType, String importName, String companyId, String accountId, String importDate) {
        ImportFailureLogExample importFailureLogExample = new ImportFailureLogExample();
        ImportFailureLogExample.Criteria criteria = importFailureLogExample.createCriteria();
        criteria.andOperateTypeEqualTo(operateType);
        if(!StringUtils.isBlank(importName)){
            criteria.andImportNameEqualTo(importName);
        }
        if(!StringUtils.isBlank(companyId)){
            criteria.andCompanyIdEqualTo(companyId);
        }
        if(!StringUtils.isBlank(accountId)){
            criteria.andAccountIdEqualTo(accountId);
        }
        criteria.andImportDataEqualTo(importDate);
        List<ImportFailureLog> list = importFailureLogMapper.selectByExampleWithBLOBs(importFailureLogExample);
        if(list.size()>0){
            return true;
        }else {
            return false;
        }
    }


}
